﻿
Ext.define('YZSoft.src.xform.FormContainer', {
    override: 'Ext.Container',
    config: {
    },

    $applyFormState: function () {
        var me = this,
            formdataset = me.config && me.config.formInfo && me.config.formInfo.formdataset,
            fields;

        if (!formdataset)
            return;

        fields = me.$getFieldsArrayBy(function (field) {
            return field.config && field.config.xdatabind;
        });

        Ext.each(fields, function (field) {
            var column = me.$getColumn(field.config && field.config.xdatabind);
            if (column)
                me.$applyWritable(field, column.Writeable);
        });
    },

    $getFormData: function () {
        var me = this,
            fields, data = {}, vars = {};

        fields = me.$getFieldsArrayBy(function (field) {
            return field.config && field.config.xdatabind;
        });

        Ext.each(fields, function (field) {
            var $var = me.$getVar(field.config && field.config.xdatabind),
                table = $var && $var.table,
                column = $var && $var.column;

            if ($var.isVar) {
                vars[$var.name] = field.getValue(), column;
                return;
            }

            if (!column || !column.Writeable)
                return;

            if (table.IsRepeatable) {
                return;
            }
            else {
                var datatable = data[table.TableName] = data[table.TableName] || [{}],
                    row = datatable[0];

                row[column.ColumnName] = me.$parseFieldValue(field.getValue(), column);
            }
        });

        return {
            vars: vars,
            FormData: data
        };
    },

    $parseFieldValue: function (value, column) {
        return value;
    },

    $applyWritable: function (field, writeable) {
        if (!writeable) {
            if (!field.getDisabled() && !field.getReadOnly())
                field.setReadOnly(true);
        }
    },

    $getFieldsArrayBy: function (fn) {
        var fields = [];

        var getFields = function (item) {
            if (fn(item)) {
                fields.push(item);
            }

            if (item.isContainer) {
                item.items.each(getFields);
            }
        };

        this.items.each(getFields);
        return fields;
    },

    $getVar: function (varname) {
        var me = this,
            varMaps = me.$varMaps || {},
            $var;

        if (!varname)
            return;

        $var = varMaps[varname];
        if ($var)
            return $var;

        $var = varMaps[varname] = me.$parseVarName(varname);
        return $var
    },

    $getColumn: function (varname) {
        var me = this,
            $var = me.$getVar(varname);

        return $var && $var.column;
    },

    $parseVarName: function (varname) {
        var me = this,
            names, len;

        if (!varname)
            return;

        names = varname.split('.');
        len = names.length;

        if (len == 0)
            return null;

        if (len == 1) {
            return {
                isVar: true,
                name: varname
            };
        }

        var tableName = names[len - 2],
            columnName = names[len - 1],
            formdataset = me.config && me.config.formInfo && me.config.formInfo.formdataset,
            $var, table, column;

        $var = {
            isVar: true,
            name: varname
        };

        table = formdataset && formdataset[tableName];
        column = table && table.Columns[columnName];
        if (column) {
            return {
                isColumn: true,
                tableName: tableName,
                columnName: columnName,
                table: table,
                column: column
            };
        }
        else {
            return {
                isVar: true,
                name: varname
            };
        }
    }
});